<html>

<head>
    <meta charset="utf-8">
    <title>websocket</title>
</head>


<script type="text/javascript">


    var ws = new WebSocket("ws://127.0.0.1:5567");

    function $value(input){
        var value = document.getElementById(input).value;

        if("" == value){
            console.log("value is default 0");
            return 0;
        }
        return parseInt(value);
    }


    // open
    ws.onopen = function(){

        console.log("connect [ws://127.0.0.1:5567] success.");

        document.getElementById("eques").addEventListener("click",function(){


            var j1 = $value("j1");
            var j2 = $value("j2");
            var postDetail = j1 + "+" + j2;
            console.log(postDetail);

            // 发送
            ws.send(postDetail);


        });



    };

    // message
    ws.onmessage = function (evt) {
        var received_msg = evt.data;

        var array = received_msg.split("-");

        document.getElementById("j1").value = array[0];
        document.getElementById("j2").value = array[1];
        document.getElementById("value").value = array[2];
        console.log(array);
    };


    // close
    ws.onclose = function(){
        console.log("close");
    };



</script>


<body>

<div style="line-height: 30px;height: 30px;">
    <input type="text" id="j1" /> + <input type="text" id="j2" />
    <button type="button" id="eques" > = </button>

    <input type="text" name="send" id="value" />
</div>





</body>



</html>